% Code adapated from this post https://uk.mathworks.com/matlabcentral/answers/347547-using-bayesopt-for-treebagger-classification
function oobErr = Optimisation(params,X,Y,par)
% oobErrRF Trains random forest and estimates out-of-bag quantile error
%   oobErr trains a random forest of 300 regression trees using the
%   predictor data in X and the parameter specification in params, and then
%   returns the out-of-bag quantile error based on the median. X is a table
%   and params is an array of OptimizableVariable objects corresponding to
%   the minimum leaf size and number of predictors to sample at each node.
randomForest = TreeBagger(params.numTrees,X,Y,...
                          'Method','classification',...
                          'OOBVarImp','On',...
                          'MinLeafSize',params.minLS);
                      
                          %'NumPredictorstoSample',params.numPTS);
                      
oobErr = oobError(randomForest, 'Mode','ensemble');
end




% testmodel = TreeBagger(20,In,Out,...
%            'Method','classification',...
%            'OOBVarImp','On',...
%            'MinLeafSize', 20,...
%            'NumPredictorstoSample', 5,...
%            'Options',par...
%            )